#! /usr/bin/python
r''' 

``base64encode.py``

    :date: 20. Okt. 2011

'''
import base64
# To change this template, choose Tools | Templates
# and open the template in the editor.

# Author: Sarah Theobald

base = {
    0 : 'A', 1 : 'B', 2: 'C', 3: 'D', 4 : 'E', 5: 'F', 6: 'G',
    7 : 'H', 8 :'I', 9 : 'J', 10 : 'K', 11 : 'L', 12 : 'M',
    13 : 'N', 14 : 'O', 15 : 'P', 16 : 'Q', 17 : 'R', 18 : 'S',
    19 : 'T', 20 : 'U', 21 : 'V', 22 : 'W', 23 : 'X', 24 : 'Y',
    25 : 'Z', 26 : 'a', 27 : 'b', 28 : 'c', 29 : 'd', 30 : 'e',
    31 : 'f', 32 : 'g', 33 : 'h', 34 : 'i', 35 : 'j', 36 : 'k',
    37 : 'l', 38 : 'm', 39 : 'n', 40 : 'o', 41 : 'p', 42 : 'q',
    43 : 'r', 44 : 's', 45 : 't', 46 : 'u', 47 : 'v', 48 : 'w',
    49 : 'x', 50 : 'y', 51 : 'z', 52 : '0', 53 : '1', 54 : '2',
    55 : '3', 56 : '4', 57 : '5', 58 : '6', 59 : '7', 60 : '8',
    61 : '9', 62 : '+', 63 : "/"
}

pad = '='

def base64encode(input_data):
    '''
	Verschluesselt die Daten in base 64

	:param input_data: Daten, die verschluesselt werden (type ``str``)
	:return: die base 64 Verschluesselung von ``input_data``
	:rtype: ``str``
    '''

    binStr = ""
    encode = ""
    versch = ""
    versch1 = ""
    for i in range(len(input_data)):
        decimal_ascii = ord(input_data[i])
        binary = bin(decimal_ascii)
        split = binary[2:]
        while len(split) < 8:
            split = '0' + split
        binStr = binStr + split

    bitlaenge = len(binStr)
    
    if(bitlaenge % 6 == 0):
        anzahl_goups = bitlaenge / 6

        z = 0
        while z <= anzahl_goups-1:
            group = '0b' + binStr[z*6:(z+1)*6]
            z = z+1

            decimal = eval(group)

            if base.has_key(decimal):
                value = base.get(decimal)
                encode = encode + value
        print encode
        return encode

    elif(bitlaenge % 6 == 2):
        anzahl_groups1 = bitlaenge / 6

        z=1
        while z <= anzahl_groups1:
            group2 = '0b' + binStr[(z-1)*6:z*6]
            z=z+1
            first = eval(group2)
            
            if base.has_key(first):
                val = base.get(first)
                versch = versch + val        
        rest = '0b' + binStr[-2:] + '0000'
        last = eval(rest)
        
        if base.has_key(last):
            va = base.get(last)

        all = versch + va + (pad*2)
        print all
        return all

    elif(bitlaenge % 6 == 4):
        anzahl_groups2 = bitlaenge / 6
        z=1
        
        while z <= anzahl_groups2:
            group3 = '0b' + binStr[(z-1)*6:z*6]
            z=z+1
            
            first1 = eval(group3)

            if base.has_key(first1):
                val1 = base.get(first1)
                versch1 = versch1 + val1

        rest1 = '0b' + binStr[-4:] + '00'
        last1 = eval(rest1)
        
        
        if base.has_key(last1):
            va1 = base.get(last1)

        all1 = versch1 + va1 + pad
        print all1
        return all1

if __name__ =="__main__":
    #test_file = 'ascii.txt'
    test_file = 'avatar.jpg'
    file_desc = open(test_file)
    test_data = file_desc.read()
    stan_codierung = base64.b64encode(test_data)
    test_codierung = base64encode(test_data)

    print 'Standard:  ' +stan_codierung
    print 'Test    :  ' + test_codierung

    if stan_codierung == test_codierung:
        print 'Base64 Test success'


    base64encode("")
    base64encode("f")
    base64encode("fo")
    base64encode("foo")
    base64encode("foob")
    base64encode("fooba")
    base64encode("foobar")


